package com.zhao.modleclass.activity;

import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zhao.modleclass.R;
import com.zhao.modleclass.adapter.HP_TagAdapter;
import com.zhao.modleclass.bean.HP_TagBean;
import com.zhao.modleclass.interfaces.IViewPager;
import com.zhao.modleclass.myview.MyLinearLayoutManager;
import com.zhao.modleclass.utils.Constant;
import com.zhao.modleclass.utils.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HPTagListActivity extends AppCompatActivity {

    @Bind(R.id.iv_Toleft_AcTTag)
    ImageView ivToleftAcTTag;
    @Bind(R.id.tv_title_AcTTag)
    TextView tvTitleAcTTag;
    @Bind(R.id.rv_HP_Tag)
    RecyclerView rvHPTag;
    @Bind(R.id.PTRS_HP_AcTTag)
    PullToRefreshScrollView PTRSHPAcTTag;
    private int page = 1;
    private List<HP_TagBean.DataBean> list = new ArrayList<HP_TagBean.DataBean>();
    private Map<String, Integer> map;
    private Retrofit retrofit;
    private HP_TagAdapter adapter;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hptaglist);
        //设置状态栏的颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //获取样式中的属性值
            TypedValue typedValue = new TypedValue();
            this.getTheme().resolveAttribute(Color.parseColor("#FF4081"), typedValue, true);
            int[] attribute = new int[]{Color.parseColor("#FF4081")};
            TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute);
            int color = array.getColor(0, Color.parseColor("#FF4081"));
            array.recycle();
            window.setStatusBarColor(color);
        }
        ButterKnife.bind(this);
        //初始化recycler
        rvHPTag.setNestedScrollingEnabled(false);
        rvHPTag.setLayoutManager(new MyLinearLayoutManager(this));
        rvHPTag.addItemDecoration(new SpacesItemDecoration(2));

        //PTRSHPAcTTag上拉下拉监听
        PTRSHPAcTTag.setMode(PullToRefreshBase.Mode.BOTH);
        PTRSHPAcTTag.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page = 1;
                loadDataFromNet(map, retrofit);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                loadDataFromNet(map, retrofit);
            }
        });
        //上页跳转监听
        ivToleftAcTTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Intent intent = getIntent();
        int tagId = intent.getIntExtra("tagId", -1);
        String tagName = intent.getStringExtra("tagName");
        tvTitleAcTTag.setText(tagName);
//        tagId=2&pageSize=10&pageNo=1
        map = new HashMap<>();
        map.put("tagId", tagId);
        map.put("pageSize", 10);


        retrofit = new Retrofit.Builder()
                .baseUrl(Constant.URL_TAG)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        loadDataFromNet(map, retrofit);
    }

    private void loadDataFromNet(Map<String, Integer> map, Retrofit retrofit) {
        map.put("pageNo", page);
        IViewPager iViewPager = retrofit.create(IViewPager.class);
        Call<HP_TagBean> hp_tagBean = iViewPager.getHP_TagBean(map);
        hp_tagBean.enqueue(new Callback<HP_TagBean>() {
            @Override
            public void onResponse(Call<HP_TagBean> call, Response<HP_TagBean> response) {
                HP_TagBean hpTagBean = response.body();
                if (response.body().getData().size() == 0) {
                    Toast.makeText(HPTagListActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
                } else {
                    list.addAll(hpTagBean.getData());
                    adapter = new HP_TagAdapter(HPTagListActivity.this, list, rvHPTag);
                    rvHPTag.setAdapter(adapter);

                    if (page == 1) {
                        list.clear();
                        list.addAll(hpTagBean.getData());
                        adapter.notifyDataSetChanged();
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                    PTRSHPAcTTag.onRefreshComplete();
                }
                PTRSHPAcTTag.onRefreshComplete();
            }

            @Override
            public void onFailure(Call<HP_TagBean> call, Throwable t) {
                Toast.makeText(HPTagListActivity.this, "网络请求失败，请检查您的网络连接", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
